#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan MongoDB (GridFS) Storage

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_36"
__date__    = "2014/01/05"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python
from bson import ObjectId

# Mongo
import pymongo
from gridfs import GridFS

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.storage import AbstractMikanBasicStorage

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan MongoDB GridFS Storage Class
#
class MikanMongoDBStorage(AbstractMikanBasicStorage):

	# ----------------------------------------------------
	## コンストラクタ
	def __init__(self, database, readonly=False):
		super(MikanMongoDBStorage, self).__init__(readonly)

		self.FDatabase = database

	def _gridfs(self, subpath):
#		return GridFS(self.database, subpath)
		return GridFS(self.database)

	def filename(self, subpath, id):
		f = self.get_file_data(subpath, id)
		return f["filename"]

	def size(self, subpath, id):
		f = self.get_file_data(subpath, id)
		return f["length"]

	def time(self, subpath, id):
		f = self.get_file_data(subpath, id)
		return f["uploadDate"]

	def get_database(self):
		return self.FDatabase

	def list_filter(self, subpath, filters):
		return filters

	def get_file_data(self, subpath, id):
		name = "fs.files"
		return self.database[name].find_one({"_id": ObjectId(id), "folder": subpath})

	def list(self, subpath):
		l = []
		name = "fs.files"
		for i in self.database[name].find({"folder": subpath}):
			l.append(i["_id"].__str__())
		return self.list_filter(subpath, l)

	def read(self, subpath, id, encode=None):
		fs = self._gridfs(subpath)
		return fs.get(ObjectId(id))

	def write(self, subpath, filename, encode=None):
		if self.isreadonly:
			return None

		fs = self._gridfs(subpath)
		return fs.new_file(filename=filename, folder=subpath)

	def put(self, subpath, filename, fstream):
		if self.isreadonly:
			return None

		fs = self._gridfs(subpath)
		return fs.put(fstream, filename=filename, folder=subpath)

	# ----------------------------------------------------
	## Property
	database = ESiPCProperty(get_database, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
